package org.ermannofranco.genericdaojdbc.user.dao;

import net.ermannofranco.genericdaojdbc.dao.AbstractGenericDao;

import org.ermannofranco.genericdaojdbc.user.model.User;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class UserDao extends AbstractGenericDao<User, Long> implements IUserDao {

	private IAddressDao addressDao;

	// public int update(User t, Long id) {
	// log.debug("updating " + id);
	// String sql =
	// "UPDATE UTENTI SET TIPO_UTENTE= :tipoUtente, NOME= :nome, ETA= :eta, LOGIN= :login, EMAIL= :email, PASSWORD= :pwd , VERSION= :newVersion WHERE USER_ID= :id AND VERSION = :version";
	// MapSqlParameterSource source = new MapSqlParameterSource("id", id);
	// source.addValue("version", t.getVersion()).addValue("tipoUtente",
	// t.getTipoUtente()).addValue("login", t.getLogin()).addValue(
	// "pwd", t.getPassword()).addValue("email", t.getEmail())
	// .addValue("eta", t.getEta()).addValue("nome", t.getNome())
	// .addValue("newVersion", t.getVersion() + 1);
	// int ret = template.update(sql, source);
	// if (ret == 0)
	// throw new OptimisticLockingFailureException(
	// "Stale data: try refreshing your copy of data, probably another transaction has changed version...");
	// return ret;
	// }

	public void setAddressDao(IAddressDao addressDao) {
		this.addressDao = addressDao;
	}

	// @Override
	// protected RowMapper<User> getRowMapper() {
	// return new RowMapper<User>() {
	//
	// public User mapRow(ResultSet rs, int rowNumber) throws SQLException {
	// User ret = new User(rs.getInt("ETA"), rs.getString("NOME"), rs
	// .getString("LOGIN"), rs.getString("PASSWORD"), rs
	// .getInt("TIPO_UTENTE"), rs.getInt("VERSION"));
	// ret.setId(rs.getLong("USER_ID"));
	// ret.setEmail(rs.getString("EMAIL"));
	// BigDecimal addrId = rs.getBigDecimal("ADDRESS_ID");
	// log.debug("addrId: " + addrId);
	// if (addrId != null) {
	// long id = addrId.longValue();
	// Address address = addressDao.getById(id);
	// ret.setAddress(address);
	// }
	// return ret;
	// }
	// };
	// }

}
